# coding=utf-8
import socket
import time

import cv2 as cv
import numpy as np

UDP_DEST_ADDR = ("127.0.0.1", 8080)
udp_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_server.bind(UDP_DEST_ADDR)

while True:
    data, client = udp_server.recvfrom(65535)
    if data is not None:
        # print type(data), len(data)
        frame = np.fromstring(data, dtype=np.uint8)
        frame = frame.reshape((120, 160, 3))
        frame = cv.resize(frame, (320, 240), cv.INTER_LANCZOS4)
        # cv.imshow("video_server", frame)
        # if cv.waitKey(1) & 0xFF == ord('q'):
        #     break
        now_time = int(round(time.time() * 10))  # 百毫秒级时间戳
        cv.imwrite("frame" + str(now_time) + ".png", frame)
